<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const cai = {
        uname: '只因',
        age: 2.5,
        hobbit: ['sing', 'dance', 'rap', 'basketball'],
        fans: {
          uname: 'ikun',
          age: 9999,
          hobbit: ['smoke', 'drink', 'makeHairStyle'],
        },
      }

      // 创建一个空的对象
      const obj = {}
      // 准备一个函数
      function deepClone(newObj, oldObj) {
        // 遍历循环要拷贝的对象
        for (let k in oldObj) {
          // 判断是不是数组
          if (oldObj[k] instanceof Array) {
            // 提前赋值
            newObj[k] = []
            deepClone(newObj[k], oldObj[k])
          }
          //判断是不是对象
          else if (oldObj[k] instanceof Object) {
            // 提前赋值
            newObj[k] = {}
            deepClone(newObj[k], oldObj[k])
          } else {
            //判断是不是简单数据类型，是的话直接赋值
            newObj[k] = oldObj[k]
          }
        }
      }
      // 调用函数
      deepClone(obj, cai)
      obj.fans.hobbit[2] = '嘿嘿嘿'
      console.log(obj)
      console.log(cai)
    </script>
  </body>
</html>
